﻿namespace MyCompany.Views {
	using System;
	using System.ComponentModel;

	using SolidMvvm;

	using ViewModels;

	public partial class LogView : IView, LogViewModel.ILogViewModelObserver {
		readonly ViewHelper<LogView, LogViewModel> viewHelper;

		public LogView() {
			InitializeComponent();

			viewHelper = new ViewHelper<LogView, LogViewModel>(this);
		}

		public void DisconnectFromViewModel() {
			viewHelper.ViewModel.LogViewModelObserver = this;
		}

		public void OnViewModelApplied() {
			// We need to let the data binding fire before scrolling to the end.
			Dispatcher.BeginInvoke((Action)Update);
			viewHelper.ViewModel.LogViewModelObserver = this;
		}

		public void OnViewModelRemoved() {
			DisconnectFromViewModel();
		}

		public void OnPropertyChanged(PropertyChangedEventArgs propertyChangedEventArgs) {
			if (propertyChangedEventArgs.PropertyName == "Log")
				Update();
		}

		void Update() {
			textBoxLog.ScrollToEnd();
		}
	}
}
